package com.lagou.config;

import com.lagou.pojo.Configuration;
import com.lagou.pojo.MappedStatement;
import com.lagou.pojo.SqlTypeEnum;
import org.dom4j.Document;
import org.dom4j.DocumentException;
import org.dom4j.Element;
import org.dom4j.io.SAXReader;

import java.io.InputStream;
import java.util.List;

public class XMLMapperBuilder {

    private Configuration configuration;

    public XMLMapperBuilder(Configuration configuration) {
        this.configuration =configuration;
    }

    public void parse(InputStream inputStream) throws DocumentException {

        Document document = new SAXReader().read(inputStream);
        Element rootElement = document.getRootElement();
        String namespace = rootElement.attributeValue("namespace");

        // 构建查询的statement
        this.buildSelectStatements(namespace,rootElement);

        // 构建添加的statement
        this.buildInsertStatements(namespace,rootElement);

        // 构建修改的statement
        this.buildUpdateStatements(namespace,rootElement);

        // 构建删除的statement
        this.buildDeleteStatements(namespace,rootElement);


    }

    void buildSelectStatements(String namespace, Element rootElement){
        List<Element> list = rootElement.selectNodes("//select");
        for (Element element : list) {
            String id = element.attributeValue("id");
            String resultType = element.attributeValue("resultType");
            String paramterType = element.attributeValue("paramterType");
            String sqlText = element.getTextTrim();
            MappedStatement mappedStatement = new MappedStatement();
            mappedStatement.setId(id);
            mappedStatement.setResultType(resultType);
            mappedStatement.setParamterType(paramterType);
            mappedStatement.setSql(sqlText);
            // 查询类型
            mappedStatement.setSqlType(SqlTypeEnum.SELECT.type);
            String key = namespace+"."+id;
            this.configuration.getMappedStatementMap().put(key,mappedStatement);
        }
    }

    void buildInsertStatements(String namespace, Element rootElement){
        List<Element> list = rootElement.selectNodes("//insert");
        for (Element element : list) {
            String id = element.attributeValue("id");
            String paramterType = element.attributeValue("paramterType");
            String sqlText = element.getTextTrim();
            MappedStatement mappedStatement = new MappedStatement();
            mappedStatement.setId(id);
            mappedStatement.setParamterType(paramterType);
            mappedStatement.setSql(sqlText);
            // 查询类型
            mappedStatement.setSqlType(SqlTypeEnum.INSERT.type);
            String key = namespace+"."+id;
            this.configuration.getMappedStatementMap().put(key,mappedStatement);
        }
    }

    void buildUpdateStatements(String namespace, Element rootElement){
        List<Element> list = rootElement.selectNodes("//update");
        for (Element element : list) {
            String id = element.attributeValue("id");
            String paramterType = element.attributeValue("paramterType");
            String sqlText = element.getTextTrim();
            MappedStatement mappedStatement = new MappedStatement();
            mappedStatement.setId(id);
            mappedStatement.setParamterType(paramterType);
            mappedStatement.setSql(sqlText);
            // 查询类型
            mappedStatement.setSqlType(SqlTypeEnum.UPDATE.type);
            String key = namespace+"."+id;
            this.configuration.getMappedStatementMap().put(key,mappedStatement);
        }
    }

    void buildDeleteStatements(String namespace, Element rootElement){
        List<Element> list = rootElement.selectNodes("//delete");
        for (Element element : list) {
            String id = element.attributeValue("id");
            String paramterType = element.attributeValue("paramterType");
            String sqlText = element.getTextTrim();
            MappedStatement mappedStatement = new MappedStatement();
            mappedStatement.setId(id);
            mappedStatement.setParamterType(paramterType);
            mappedStatement.setSql(sqlText);
            // 查询类型
            mappedStatement.setSqlType(SqlTypeEnum.DELETE.type);
            String key = namespace+"."+id;
            this.configuration.getMappedStatementMap().put(key,mappedStatement);
        }


    }

}
